<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button v-if="isAuth('sys:college:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button> -->
        <el-button v-if="isAuth('sys:college:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <!-- <el-button type="primary" size="small" @click="submitNext()">提交给校级</el-button> -->
      </el-form-item>
      <el-form-item>
        <div class="getExcel">
          <download-excel :data="excelData" :fields="json_fields" :header="excelTitle" :name="excelFName">
            <el-button type="primary" :disabled="excelBtnDis" @click="isEmtpy()" size="small">全部导出为excel</el-button>
          </download-excel>
          <el-form class="excelForm" :label-position="'left'">
            <el-form-item label="excel表标题:">
              <el-input size="small" placeholder="表格标题" style="width:160px" v-model="excelTitle"></el-input>
            </el-form-item>
            <el-form-item label="excel文件名:">
              <el-input size="small" placeholder="文件名" style="width:160px" v-model="excelFName">
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="项目id" width="75">
      </el-table-column>
      <el-table-column prop="college" header-align="center" align="center" label="所属学院">
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="项目标题">
      </el-table-column>
      <el-table-column prop="outline" header-align="center" align="center" label="项目概要">
      </el-table-column>
      <el-table-column prop="file" header-align="center" align="center" label="文件名">
      </el-table-column>
      <el-table-column prop="leader" header-align="center" align="center" label="负责人">
        <template slot-scope="scope">
          <div>{{scope.row.leader}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="teacherName" header-align="center" align="center" label="指导老师">
      </el-table-column>
      <el-table-column prop="average" header-align="center" align="center" label="平均分">
      </el-table-column>
      <el-table-column prop="peopleNumber" header-align="center" align="center" label="评分总人数">
      </el-table-column>
      <el-table-column prop="statu" header-align="center" align="center" label="评审状态">


        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.statu==1">通过</el-tag>
          <el-tag type="danger" v-else-if="scope.row.statu==2">不通过</el-tag>

          <el-select v-else v-model="scope.row.statu" placeholder="请选择" @change="change(scope.row.id,scope.row.statu)">
            <el-option v-for="item in status" :key="item.id" :label="item.statu" :value="item.id">
            </el-option>
          </el-select>

        </template>


      </el-table-column>
      <el-table-column prop="projectType" header-align="center" align="center" label="比赛类型">
      </el-table-column>
      <el-table-column prop="type" header-align="center" align="center" label="项目类型">
      </el-table-column>
<!--      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
      </el-table-column> -->
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

  </div>
</template>

<script>
  import AddOrUpdate from './college-add-or-update'
  export default {
    data() {
      return {
        status: [{
          id: 0,
          statu: '审核中',
        }, {
          id: 1,
          statu: '通过',
        }, {
          id: 2,
          statu: '不通过',
        }, ],
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,

        json_fields: {
          '项目标题': 'title',
          '项目类型': 'type',
          '项目年限': 'projectType',
          '项目概要': 'outline',
          '负责人': 'leader',
          '指导老师': 'teacherName',
          '立项材料': 'file',
          '平均分': 'average',
          '评分人数': 'peopleNumber'
        },
        excelTitle: '',
        excelFName: '',
        excelData: [],
        ids: [],
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
    },
    computed: {
      excelBtnDis() {
        if ((this.excelTitle !== '') && (this.excelFName !== '') && (this.excelData.length !== 0)) return false
        return true
      },
    },
    methods: {
      change(id, statu) {
        this.$confirm('提交后无法修改', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true,
        }).then(()=>{
          this.submitNext(id, statu)
        }).catch((action)=>{
          if(action=='cancel') {console.log('cancel');}
        })


      },
      test(i) {
        console.log('quxiao', i);
        this.dialogVisible = false
      },

      tijiao(id, statu) {
        this.$http({
          url: this.$http.adornUrl('/sys/college/refer'),
          method: 'post',
          params: {
            id,
            statu
          }
        }).then(({
          data
        }) => {
          if (data.code == 0) {
            this.$message.success('提交成功')
            this.getDataList()
          } else {
            this.$message.error('提交失败' + data.msg)
          }
        })
      },

      //提交给校级
      submitNext(id, statu) {
        this.$http({
          url: this.$http.adornUrl('/sys/college/refer'),
          method: 'post',
          params: {
            id,
            statu
          }
        }).then(({
          data
        }) => {
          if (data.code == 0) {
            this.$message.success('提交成功')
            this.getDataList()
            this.dialogVisible = false
          } else {
            this.$message.error('提交失败' + data.msg)
          }
        })

      },

      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/college/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            // 'userId':
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
        this.excelData = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/college/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      isEmtpy() {
        if (this.excelData.length == 0) {
          this.$message.warning('请先选择要导出的项目')
        }
      },
    }
  }
</script>


<style lang="scss">
  .getExcel {
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    // background-color: #000;
    // justify-content: space-between;
    // margin: 10px 0;
    background-color: #fff;
    border-radius: 15px;
    padding: 0px 20px;
    box-sizing: border-box;

    .excelForm {
      // display: flex;
      // padding: 10px 0;
      // height: 60px;
      // background-color: #f2f3f5;
      box-sizing: border-box;
      width: 600px;

    }
  }
</style>
